﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;

namespace GRWL.BZSC.UI
{
    public partial class ProductProjectFrm : BaseFrm
    {
        /// <summary>
        /// 当前编码规则
        /// </summary>
        public CodeRuleFrm.CodeRule Rule { get; set; }

        /// <summary>
        /// 当前浏览产品工程临时变量
        /// </summary>
        TB_ProductProject ProductProject { get; set; }

        public ProductProjectFrm()
        {
            InitializeComponent();

            this.tvLeft.ImageList = this.imageList1;

            this.BaseDataProxy = new GRWL.BZSC.UI.BaseDataSvc.BaseDataClient();
        }

        private void ProductProjectFrm_Load(object sender, EventArgs e)
        {
            this.Rule = getCurrentCodeRule(true);
            GRWL.BZSC.Common.EventCommon.PrdtPrjtUdTVEvnt += new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(prjUpdate);
            GRWL.BZSC.Common.EventCommon.SubPrdtPrjtUdTVEvnt += new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(subPrjUpdate);
            GRWL.BZSC.Common.EventCommon.CopyPrjtUdTVEvnt += new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(copyPrjtUpdate);
            GRWL.BZSC.Common.EventCommon.PrjtWorkstageUdTVEvnt += new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(prjtWorkstageUpdate);
            GRWL.BZSC.Common.EventCommon.PrjtElementUdTVEvnt += new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(PrjtElementUpdate);
            this.BaseDataProxy.Open();
            bindAllPrjt();
        }

        /// <summary>
        /// 获取当前编码规则
        /// </summary>
        /// <param name="init"></param>
        /// <returns></returns>
        private CodeRuleFrm.CodeRule getCurrentCodeRule(bool init)
        {
            if (init)
            {
                if (this.Rule == null)
                    this.Rule = new CodeRuleFrm.CodeRule();
                this.Rule.addParent = false;
                this.Rule.autoCode = true;
                this.Rule.autoFill = true;
                this.Rule.header = "NO.";
                this.Rule.len = 3;
            }

            return this.Rule;
        }

        #region 外部委托回调
        private void copyPrjtUpdate(object o, EventArgs arg)
        {
            TreeNode node = this.tvRight.SelectedNode;
            if (node != null)
            {
                CommonEventArg<Int32> argObj = (CommonEventArg<Int32>)arg;
                bindBOMTree(ref node, Convert.ToInt32(argObj.t));
            }
        }

        /// <summary>
        /// 工程更新回调
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private void prjUpdate(object o, EventArgs arg)
        {
            bindAllPrjt();
            CommonEventArg<XPrdtPrjt> argObj = (CommonEventArg<XPrdtPrjt>)arg;
            if (argObj != null)
                bindRightPrjt(argObj.t.PrdtPrjt.id);
        }

        /// <summary>
        /// 子工程更新回调
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private void subPrjUpdate(object o, EventArgs arg)
        {
            TreeNode node = this.tvRight.SelectedNode;
            if (node != null)
            {
                CommonEventArg<XPrdtPrjt> argObj = (CommonEventArg<XPrdtPrjt>)arg;
                if (argObj != null)
                {
                    if (argObj.OpType == Common.OperateType.Add)
                    {
                        node.Nodes.Add(getPrdtPrjtNode(argObj.t));
                        //绑定下级工程
                        XPrjtElement e = (XPrjtElement)(XPrjtElement)node.Tag;
                        e.PrjtElement.nextPrjt = argObj.t.PrdtPrjt.id;
                        e.PrjtElement.nextPrjtCode = argObj.t.PrdtPrjt.code;
                        this.BaseDataProxy.UpdatePrjtElement(e.PrjtElement);
                    }
                    else if (argObj.OpType == Common.OperateType.Update)
                    {
                        TreeNode nNode = getPrdtPrjtNode(argObj.t);
                        node.Text = nNode.Text;
                        node.Tag = nNode.Tag;
                        node.Name = nNode.Name;
                        node.ImageIndex = nNode.ImageIndex;
                    }
                }
            }
        }

        /// <summary>
        /// 工程工序更新回调
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private void prjtWorkstageUpdate(object o, EventArgs arg)
        {
            //TreeNode node = this.tvRight.SelectedNode;
            //if (node != null)
            //{
                CommonEventArg<XPrjtWorkstage> argObj = (CommonEventArg<XPrjtWorkstage>)arg;
                if (argObj != null)
                {
                    bindRightPrjt(Convert.ToInt32(argObj.t.PrjtWorkstage.project));
                    //if (argObj.OpType == Common.OperateType.Add)
                    //{
                    //    //node.Nodes.Add(getPrjtWorkstageNode(argObj.t));
                    //}
                    //else if (argObj.OpType == Common.OperateType.Update)
                    //{                          
                    //    //TreeNode pNode = node.Parent;
                    //    //pNode.Nodes.Remove(node);
                    //    //pNode.Nodes.Add(getPrjtWorkstageNode(argObj.t));
                    //}
                    //else if (argObj.OpType == Common.OperateType.Delete)
                    //{
                    //    //TreeNode pNode = node.Parent;
                    //    //pNode.Nodes.Remove(node);
                    //}
                }
            //}
        }

        /// <summary>
        /// 工程元件更新回调
        /// </summary>
        /// <param name="o"></param>
        /// <param name="arg"></param>
        private void PrjtElementUpdate(object o, EventArgs arg)
        {
            //TreeNode node = this.tvRight.SelectedNode;
            //if (node != null)
            //{
                CommonEventArg<XPrjtElement> argObj = (CommonEventArg<XPrjtElement>)arg;
                if (argObj != null)
                {
                    bindRightPrjt(Convert.ToInt32(argObj.t.PrjtId));
                    //if (argObj.OpType == Common.OperateType.Add)
                    //{
                    //    //node.Nodes.Add(getPrjtElementNode(argObj.t));
                    //}
                    //else if (argObj.OpType == Common.OperateType.Update)
                    //{
                    //    //TreeNode pNode = node.Parent;
                    //    //pNode.Nodes.Remove(node);
                    //    //pNode.Nodes.Add(getPrjtElementNode(argObj.t));
                    //}
                    //else if (argObj.OpType == Common.OperateType.Delete)
                    //{
                    //    //TreeNode pNode = node.Parent;
                    //    //pNode.Nodes.Remove(node);
                    //}
                }
            //}
        }
        #endregion

        /// <summary>
        /// 左边绑定所有产品工程
        /// </summary>
        private void bindAllPrjt()
        {
            if (this.tvLeft.Nodes != null)
                if (this.tvLeft.Nodes.Count > 0)
                    this.tvLeft.Nodes.Clear();
            TreeNode node = new TreeNode("所有类别");
            node.ImageIndex = 0;
            TB_Options[] options = this.BaseDataProxy.GetOptionTree(GRWL.BZSC.Common.EnumCommon.OptionType.物料分类.ToString());
            if (options != null)
            {
                initChild(node, options, 0);
            }
            this.tvLeft.Nodes.Add(node);
        }

        private void initChild(TreeNode node, TB_Options[] options, int parentId)
        {
            var ops = from o in options where o.parentId == parentId select o;
            if (options != null)
            {
                XPrdtPrjt[] ary = this.BaseDataProxy.GetPrdtPrjts();
                foreach (TB_Options child in ops)
                {
                    TreeNode newNode = node.Nodes.Add(child.id.ToString(), child.code + "[" + child.name + "]");
                    newNode.Tag = child.code;
                    newNode.ImageIndex = 1;
                    newNode.Name = child.id.ToString();
                    loadPrjtNode(newNode, ary);
                    initChild(newNode, options, child.id);
                }
            }
        }

        /// <summary>
        /// 加载物料工程节点
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="ary"></param>
        private void loadPrjtNode(TreeNode parentNode, XPrdtPrjt[] ary)
        {
            int? category = Convert.ToInt32(parentNode.Name);

            if (ary != null)
            {
                //this.tvLeft.SelectedNode = node;

                int? currentMtrlId = 0;//当前物料Id
                TreeNode mNode = new TreeNode();
                foreach (XPrdtPrjt q in ary)
                {
                    bool mNodeCreate = q.CategoryId == category && currentMtrlId != q.PrdtPrjt.material;//创建物料节点标记
                    if (mNodeCreate)
                    {
                        mNode = new TreeNode();
                        mNode.Name = q.MaterialName;
                        mNode.Tag = q.MaterialCode;
                        mNode.Text = "[" + q.MaterialCode + "]" + q.MaterialName + "|" + q.Specs;
                        mNode.ImageIndex = 2;
                        currentMtrlId = q.PrdtPrjt.material;
                    }
                    if (q.CategoryId == category && currentMtrlId == q.PrdtPrjt.material)
                    {
                        TreeNode prjtNode = new TreeNode();
                        prjtNode.Name = q.PrdtPrjt.id.ToString();
                        prjtNode.Tag = q;
                        prjtNode.Text = "[" + q.PrdtPrjt.code + "]版本号:" + q.PrdtPrjt.version;
                        prjtNode.ImageIndex = 3;
                        mNode.Nodes.Add(prjtNode);
                    }
                    if (mNodeCreate)
                        parentNode.Nodes.Add(mNode);

                }
            }
        }
        
        /// <summary>
        /// 避免treeview选中后显示默认的图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvLeft_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.SelectedImageIndex != e.Node.ImageIndex)
                e.Node.SelectedImageIndex = e.Node.ImageIndex;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            TreeViewLookUp.LookUpNode(this.tvLeft, LookUpType.ADOWN_LOOK, this.txtBoxKey.Text);
        }

        private void tvLeft_ItemDrag(object sender, ItemDragEventArgs e)
        {
            //Move   the   dragged   node   when   the   left   mouse   button   is   used
            if (e.Button == MouseButtons.Left)
            {
                TreeNode node = (TreeNode)e.Item;
                if (node.ImageIndex == 3)
                {
                    DoDragDrop(e.Item, DragDropEffects.Move);
                    this.tvLeft.SelectedNode = node;
                }
            }
            else if (e.Button == MouseButtons.Right)// Copy   the   dragged   node   when   the   right   mouse   button   is   used.
            {
                DoDragDrop(e.Item, DragDropEffects.Copy);
            }

        }

        /// <summary>
        /// Set   the   target   drop   effect   to   the   effect specified   in   the   ItemDrag   event   handler.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvLeft_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = e.AllowedEffect;

        }

        private void tvRight_DragEnter(object sender, DragEventArgs e)
        {
            //   Retrieve   the   node   that   was   dragged.   
            TreeNode node = (TreeNode)e.Data.GetData(typeof(TreeNode));
            if (node != null)
                e.Effect = DragDropEffects.Move;
            else
                Cursor = Cursors.No;
        }

        private void tvRight_DragDrop(object sender, DragEventArgs e)
        {
            //   Retrieve   the   node   that   was   dragged.   
            TreeNode node = (TreeNode)e.Data.GetData(typeof(TreeNode));
            bindRightPrjt(Convert.ToInt32(node.Name));
        }

        /// <summary>
        /// 右边绑定产品工程BOM树
        /// </summary>
        /// <param name="prjtId"></param>
        private void bindRightPrjt(int prjtId)
        {
            XPrdtPrjt xPrdtPrjt = this.BaseDataProxy.GetXPrdtPrjt(prjtId);
            if (xPrdtPrjt != null)
            {
                TreeNode root = getPrdtPrjtNode(xPrdtPrjt);
                bindBOMTree(ref root, xPrdtPrjt.PrdtPrjt.id);
                this.tvRight.Nodes.Clear();
                this.tvRight.Nodes.Add(root);
                this.tvRight.ExpandAll();
            }
        }

        /// <summary>
        /// 绑定BOM树
        /// 算法-先加载物料产品工程需要几道工序，每道工序中需要多少元件，若元件有下级工程，则继续加载下级产品工程
        /// </summary>
        /// <param name="pNode">上级节点</param>
        /// <param name="prdtPrjtId">产品工程Id</param>
        private void bindBOMTree(ref TreeNode pNode, int prjtId)
        {
            //加载产品工程工序
            XPrjtWorkstage[] ary = this.BaseDataProxy.FindPrjtWorkstages(prjtId);
            if (ary != null)
            {
                foreach (XPrjtWorkstage o in ary)
                {
                    TreeNode wsNode = getPrjtWorkstageNode(o);
                    loadPrjtElements(ref wsNode, o.PrjtWorkstage.id);
                    pNode.Nodes.Add(wsNode);
                }
            }
        }


        /// <summary>
        /// 加载工程工序元件
        /// </summary>
        /// <param name="pNode"></param>
        /// <param name="workstageId"></param>
        private void loadPrjtElements(ref TreeNode pNode, int workstageId)
        {
            XPrjtElement[] elmts = this.BaseDataProxy.FindPrjtElements(workstageId);
            if (elmts != null)
            {
                foreach (XPrjtElement e in elmts)
                {
                    TreeNode elmtNode = getPrjtElementNode(e);
                    pNode.Nodes.Add(elmtNode);
                    //若有下级工程则继续加载下级产品工程                         
                    //TB_ProductProject pp = this.BaseDataProxy.GetSubPrdtPrjt(e.Id);//工程提交后不能再引用其他工程，可供选择引用的是已提交审批的工程，所以不存在死循环的情况
                    if (e.NextPrjtId != null)
                    {
                        //elmtNode. = e.MaterialCode;
                        elmtNode.ImageIndex = 7;
                        bindBOMTree(ref elmtNode, Convert.ToInt32(e.NextPrjtId));
                    }
                }
            }
        }

        /// <summary>
        /// 构造工程树节点
        /// </summary>
        private TreeNode getPrdtPrjtNode(XPrdtPrjt p)
        {
            //"[" + prjt.MaterialCode + "]" + prjt.MaterialName + "|" + prjt.Specs;"[" + prjt.Code + "]版本号:" + prjt.Version;
            TreeNode root = new TreeNode();
            root.Text = "[" + p.MaterialCode + "]" + p.MaterialName + "|" + p.Specs + "| [" + p.PrdtPrjt.code + "]" + p.PrdtPrjt.version;
            root.Tag = p;//工程实体
            root.Name = p.PrdtPrjt.id.ToString();//存放工程Id
            //root.ImageKey = pNode.Tag.ToString();
            root.ImageIndex = 4;
            return root;
        }

        /// <summary>
        /// 构造工程工序树节点
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        private TreeNode getPrjtWorkstageNode(XPrjtWorkstage o)
        {
            TreeNode wsNode = new TreeNode();//工程工序
            wsNode.Text = o.PrjtWorkstage.sort.ToString() + "|" + o.WorkstageCode + "[" + o.WorkstageName + "]";
            wsNode.Tag = o;
            wsNode.Name = o.PrjtWorkstage.id.ToString();
            wsNode.ImageIndex = 5;
            return wsNode;
        }

        /// <summary>
        /// 构造工程元件树节点
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        private TreeNode getPrjtElementNode(XPrjtElement e)
        {
            TreeNode elmtNode = new TreeNode();
            elmtNode.Text = e.PrjtElement.sort.ToString() + "|" + e.PrjtElement.materialCode + "[" + e.MaterialName + "|规格:" + e.Specs + "|用量:" + e.PrjtElement.dosage.ToString() + "]";
            elmtNode.Name = e.PrjtElement.id.ToString();
            elmtNode.Tag = e;
            elmtNode.ImageIndex = 6;
            return elmtNode;
        }

        /// <summary>
        /// 避免treeview选中后显示默认的图标
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tvRight_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.SelectedImageIndex != e.Node.ImageIndex)
                e.Node.SelectedImageIndex = e.Node.ImageIndex;
        }

        private void tvRight_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Point ClickPoint = new Point(e.X, e.Y);
                TreeNode currentNode = this.tvRight.GetNodeAt(ClickPoint);
                if (currentNode != null)
                {
                    this.tvRight.SelectedNode = currentNode;
                    bindMenuStrip(currentNode);
                }
            }
        }

        #region 动态绑定右键菜单
        /// <summary>
        /// 绑定菜单项
        /// </summary>
        /// <param name="node">当前选择node</param>
        private void bindMenuStrip(TreeNode node)
        {
            this.ctMenuStrip.Items.Clear();
            int nodeImgId = node.ImageIndex;
            switch (node.ImageIndex)
            {
                case 5://工序
                    XPrjtWorkstage pwsNode = (XPrjtWorkstage)node.Tag;
                    if (pwsNode != null)
                    {
                        this.ProductProject = this.BaseDataProxy.GetPrdtPrjt(Convert.ToInt32(pwsNode.PrjtWorkstage.project));
                        if (this.ProductProject != null)
                        {
                            Common.AddContextMenu("浏览工序", ctMenuStrip.Items, ctMenuClick_ViewPrjtWorkstage, true, false);
                            Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                            Common.AddContextMenu("新增元件", ctMenuStrip.Items, ctMenuClick_AddPrjtElement, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                            Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                            Common.AddContextMenu("删除工序", ctMenuStrip.Items, ctMenuClick_DeletePrjtWorkstage, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                        }
                    }
                    break;
                case 6://元件
                    XPrjtElement peNode1 = (XPrjtElement)node.Tag;
                    if (peNode1 != null)
                    {
                        this.ProductProject = this.BaseDataProxy.GetPrdtPrjt(Convert.ToInt32(peNode1.PrjtId));
                        if (this.ProductProject != null)
                        {
                            Common.AddContextMenu("浏览元件", ctMenuStrip.Items, ctMenuClick_ViewPrjtElement, true, false);
                            Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                            Common.AddContextMenu("删除元件", ctMenuStrip.Items, ctMenuClick_DeleteElement, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                            Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                            Common.AddContextMenu("新增工程", ctMenuStrip.Items, ctMenuClick_AddSubPrjt, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                            Common.AddContextMenu("向上新增元件", ctMenuStrip.Items, null, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                            Common.AddContextMenu("向下新增元件", ctMenuStrip.Items, null, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                            Common.AddContextMenu("放产确认", ctMenuStrip.Items, ctMenuClick_ReleaseElement, true, false);
                            Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                            Common.AddContextMenu("引用工程", ctMenuStrip.Items, ctMenuClick_RefPrjt, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                        }
                    }
                    break;
                case 7://子工程
                    XPrjtElement peNode = (XPrjtElement)node.Tag;
                    if (peNode != null)
                    {
                        this.ProductProject = this.BaseDataProxy.GetPrdtPrjt(Convert.ToInt32(peNode.PrjtId));
                        if (peNode.NextPrjtId != null)
                        {
                            bool isSubmit = Convert.ToBoolean(peNode.NextPrjtIsSubmit);
                            bool isVerify = Convert.ToBoolean(peNode.NextPrjtIsVerify);

                            if (this.ProductProject != null)
                            {
                                Common.AddContextMenu("浏览元件", ctMenuStrip.Items, ctMenuClick_ViewPrjtElement, true, false);
                                Common.AddContextMenu("浏览工程", ctMenuStrip.Items, ctMenuClick_ViewSubPrjt, true, false);
                                Common.AddContextMenu("浏览图档", ctMenuStrip.Items, null, true, false);
                                Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                                //已提交的下级工程不显示该菜单，确认前提示是否解除下级工程
                                Common.AddContextMenu("删除元件", ctMenuStrip.Items, ctMenuClick_DeleteElement2, !isSubmit && !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);//提示是否同时删除相关的下级工程
                                Common.AddContextMenu("删除工程", ctMenuStrip.Items, ctMenuClick_DeleteSubPrjt, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);//删除下级工程
                                Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                                Common.AddContextMenu("新增工序", ctMenuStrip.Items, ctMenuClick_AddSubPrjtWorkstage, !isVerify && !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                                Common.AddContextMenu("向上新增元件", ctMenuStrip.Items, null, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                                Common.AddContextMenu("向下新增元件", ctMenuStrip.Items, null, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                                Common.AddContextMenu("放产确认", ctMenuStrip.Items, ctMenuClick_ReleaseElement, true, false);
                                Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                                Common.AddContextMenu("引用工程", ctMenuStrip.Items, ctMenuClick_RefPrjt, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                            }
                        }
                    }
                    break;
                default://产品工程
                    //PrdtPrjtNode prdtPrjtNode = (PrdtPrjtNode)node.Tag;

                    this.ProductProject = this.BaseDataProxy.GetPrdtPrjt(Convert.ToInt32(node.Name));
                    if (this.ProductProject != null)
                    {
                        Common.AddContextMenu("浏览工程", ctMenuStrip.Items, ctMenuClick_ViewPrjt, true, false);
                        Common.AddContextMenu("浏览图档", ctMenuStrip.Items, null, true, false);
                        Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                        Common.AddContextMenu("新增工序", ctMenuStrip.Items, ctMenuClick_AddPrjtWorkstage, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废, false);
                        //Common.AddContextMenu("新增元件", ctMenuStrip.Items, null);
                        Common.AddContextMenu("放产确认", ctMenuStrip.Items, ctMenuClick_ReleasePrjt, true, this.ProductProject.isRelease);
                        Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                        Common.AddContextMenu("删除工程", ctMenuStrip.Items, ctMenuClick_DeletePrjt, !this.ProductProject.isSubmit || (this.ProductProject.isSubmit && this.ProductProject.verifyStatus == (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.作废), false);
                        Common.AddContextMenu("激活工程", ctMenuStrip.Items, ctMenuClick_ActivePrjt, this.ProductProject.submitStatus == (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废 || this.ProductProject.verifyStatus == (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.作废, false);
                        Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                        Common.AddContextMenu("提交工程", ctMenuStrip.Items, ctMenuClick_SubmitPrjt, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.作废 && this.ProductProject.verifyStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.作废, false);
                        Common.AddContextMenu("退回提交", ctMenuStrip.Items, ctMenuClick_ReturnSubmit, this.ProductProject.isSubmit && !this.ProductProject.isVerify && this.ProductProject.verifyStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.作废, false);
                        Common.AddContextMenu("作废提交", ctMenuStrip.Items, ctMenuClick_InvalidSubmit, !this.ProductProject.isSubmit && this.ProductProject.submitStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废 && this.ProductProject.verifyStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.作废, false);
                        Common.AddContextMenu("", ctMenuStrip.Items, null, true, false);
                        Common.AddContextMenu("审批工程", ctMenuStrip.Items, ctMenuClick_VerifyPrjt, this.ProductProject.isSubmit && !this.ProductProject.isVerify && this.ProductProject.verifyStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.作废, false);
                        Common.AddContextMenu("反审批", ctMenuStrip.Items, ctMenuClick_DeVerify, this.ProductProject.isVerify, false);
                        Common.AddContextMenu("退回审批", ctMenuStrip.Items, ctMenuClick_ReturnSubmit, this.ProductProject.isSubmit && !this.ProductProject.isVerify && this.ProductProject.verifyStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.作废, false);
                        Common.AddContextMenu("作废审批", ctMenuStrip.Items, ctMenuClick_InvalidVerify, this.ProductProject.isSubmit && !this.ProductProject.isVerify && this.ProductProject.verifyStatus != (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.作废, false);
                    }
                    break;
            }
            node.ContextMenuStrip = this.ctMenuStrip;
        }
        /// <summary>
        /// 浏览工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_ViewPrjt(object sender, EventArgs e)
        {
            if (this.tvRight.SelectedNode != null)
            {
                EditPrdtPrjtFrm frm = new EditPrdtPrjtFrm();
                frm.Id = Convert.ToInt32(this.tvRight.SelectedNode.Name);
                frm.EditMode = true;
                frm.ShowDialog(this);
            }
        }

        /// <summary>
        /// 浏览工程工序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_ViewPrjtWorkstage(object sender, EventArgs e)
        {
            if (this.tvRight.SelectedNode != null)
            {
                XPrjtWorkstage pwNode = (XPrjtWorkstage)this.tvRight.SelectedNode.Tag;
                EditPrjtWorkstageFrm frm = new EditPrjtWorkstageFrm();
                frm.Id = pwNode.PrjtWorkstage.id;
                frm.PrjtId = Convert.ToInt32(pwNode.PrjtWorkstage.project);
                frm.PrjtCode = pwNode.PrjtWorkstage.prjtCode;
                frm.EditMode = true;
                frm.ShowDialog(this);
            }
        }

        /// <summary>
        /// 浏览工程元件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_ViewPrjtElement(object sender, EventArgs e)
        {
            if (this.tvRight.SelectedNode != null)
            {
                XPrjtElement peNode = (XPrjtElement)this.tvRight.SelectedNode.Tag;
                EditPrjtElementFrm frm = new EditPrjtElementFrm();

                frm.Id = peNode.PrjtElement.id;
                frm.PrjtId = Convert.ToInt32(peNode.PrjtId);
                frm.PrjtCode = peNode.PrjtCode;
                frm.WorkstageSort = peNode.MachiningSort;
                frm.PrjtWorkstageId = Convert.ToInt32(peNode.PrjtElement.projectWorkstage);

                frm.EditMode = true;
                frm.ShowDialog(this);
            }
        }

        /// <summary>
        /// 新增工程工序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_AddPrjtWorkstage(object sender, EventArgs e)
        {
            if (this.tvRight.SelectedNode != null)
            {
                EditPrjtWorkstageFrm frm = new EditPrjtWorkstageFrm();
                //frm.Id = Convert.ToInt32(this.tvRight.SelectedNode.Name);
                frm.PrjtId = Convert.ToInt32(this.tvRight.SelectedNode.Name);
                frm.PrjtCode = ((XPrdtPrjt)this.tvRight.SelectedNode.Tag).PrdtPrjt.code;
                frm.EditMode = false;
                frm.ShowDialog(this);
            }
        }

        /// <summary>
        /// 删除工序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_DeletePrjtWorkstage(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除该项目吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (this.BaseDataProxy.DeletePrjtWorkstage(Convert.ToInt32(this.tvRight.SelectedNode.Name)))
                {
                    this.tvRight.SelectedNode.Nodes.Clear();
                    this.tvRight.SelectedNode.Parent.Nodes.RemoveAt(this.tvRight.SelectedNode.Index);
                }
            }
        }

        /// <summary>
        /// 新增工程元件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_AddPrjtElement(object sender, EventArgs e)
        {
            if (this.tvRight.SelectedNode != null)
            {
                XPrjtWorkstage pwsNode = (XPrjtWorkstage)this.tvRight.SelectedNode.Tag;
                EditPrjtElementFrm frm = new EditPrjtElementFrm();
                frm.PrjtId = Convert.ToInt32(pwsNode.PrjtWorkstage.project);
                frm.PrjtCode = pwsNode.PrjtWorkstage.prjtCode;
                frm.WorkstageSort = pwsNode.PrjtWorkstage.sort;
                frm.PrjtWorkstageId = pwsNode.PrjtWorkstage.id;
                frm.EditMode = false;
                frm.ShowDialog(this);
            }
        }

        /// <summary>
        /// 引用工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_RefPrjt(object sender, EventArgs e)
        {
            TreeNode node = this.tvRight.SelectedNode;
            if (node != null)
            {
                FindPrdtPrjtFrm frm = new FindPrdtPrjtFrm(node.ToolTipText);
                frm.IsVerify = true;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    if (frm.SelectorItem != null)
                    {
                        TB_ProjectElement obj = this.BaseDataProxy.GetPrjtElement(Convert.ToInt32(node.Name));
                        if (obj != null)
                        {
                            obj.nextPrjt = frm.SelectorItem.Id;
                            obj.nextPrjtCode = frm.SelectorItem.Code;
                            bool ok = false;
                            ok = this.BaseDataProxy.UpdatePrjtElement(obj);
                            if (ok)
                            {
                                node.Nodes.Clear();//先清空原有子节点
                                node.ImageIndex = 7;
                                bindBOMTree(ref node, Convert.ToInt32(obj.nextPrjt));
                            }
                        }
                    }
                }

            }
        }

        /// <summary>
        /// 删除工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_DeletePrjt(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除该项目吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (this.BaseDataProxy.DeletePrdtPrjt(Convert.ToInt32(this.tvRight.SelectedNode.Name)))
                {
                    bindAllPrjt();
                    this.tvRight.Nodes.Clear();
                }
            }
        }

        /// <summary>
        /// 激活工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_ActivePrjt(object sender, EventArgs e)
        {
            if (this.ProductProject.isSubmit)
            {
                this.ProductProject.submitStatus = (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.已提交;
            }
            else
            {
                this.ProductProject.submitStatus = (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.未提交;
            }
            this.ProductProject.verifyStatus = (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.未批准;
            this.BaseDataProxy.UpdatePrdtPrjt(this.ProductProject);
        }

        /// <summary>
        /// 产品工程放产确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_ReleasePrjt(object sender, EventArgs e)
        {
            this.ProductProject.isRelease = !this.ProductProject.isRelease;
            this.BaseDataProxy.UpdatePrdtPrjt(this.ProductProject);
        }

        /// <summary>
        /// 提交工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_SubmitPrjt(object sender, EventArgs e)
        {
            this.ProductProject.isSubmit = true;
            this.ProductProject.submitStatus = (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.已提交;
            this.BaseDataProxy.UpdatePrdtPrjt(this.ProductProject);
        }

        /// <summary>
        /// 退回提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_ReturnSubmit(object sender, EventArgs e)
        {
            this.ProductProject.isSubmit = false;
            this.ProductProject.submitStatus = (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.未提交;
            this.BaseDataProxy.UpdatePrdtPrjt(this.ProductProject);
        }

        /// <summary>
        /// 作废提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_InvalidSubmit(object sender, EventArgs e)
        {
            this.ProductProject.submitStatus = (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus.作废;
            this.BaseDataProxy.UpdatePrdtPrjt(this.ProductProject);
        }

        /// <summary>
        /// 审批工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_VerifyPrjt(object sender, EventArgs e)
        {
            XPrdtPrjt xp = (XPrdtPrjt)this.tvRight.SelectedNode.Tag;
            if (xp != null)
            {
                if (this.BaseDataProxy.VerifyPrdtPrjt(xp.PrdtPrjt.id, xp.PrdtPrjt.code, true, (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.已批准))
                {
                    MessageBox.Show("产品工程已批准！");
                }
            }

        }

        /// <summary>
        /// 反审批工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_DeVerify(object sender, EventArgs e)
        {
            XPrdtPrjt xp = (XPrdtPrjt)this.tvRight.SelectedNode.Tag;
            if (xp != null)
            {
                if (this.BaseDataProxy.VerifyPrdtPrjt(xp.PrdtPrjt.id, xp.PrdtPrjt.code, false, (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.未批准))
                {
                    MessageBox.Show("产品工程反审批成功！");
                }
            }
        }

        /// <summary>
        /// 作废审批
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_InvalidVerify(object sender, EventArgs e)
        {
            this.ProductProject.verifyStatus = (short)GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus.作废;
            this.BaseDataProxy.UpdatePrdtPrjt(this.ProductProject);
        }

        /// <summary>
        /// 删除工程元件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_DeleteElement(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除该项目吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (this.BaseDataProxy.DeletePrjtElement(Convert.ToInt32(this.tvRight.SelectedNode.Name)))
                {
                    this.tvRight.SelectedNode.Nodes.Clear();
                    this.tvRight.SelectedNode.Parent.Nodes.RemoveAt(this.tvRight.SelectedNode.Index);
                }
            }
        }

        /// <summary>
        /// 新增子工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_AddSubPrjt(object sender, EventArgs e)
        {
            XPrjtElement xp = (XPrjtElement)this.tvRight.SelectedNode.Tag;
            EditPrdtPrjtFrm frm = new EditPrdtPrjtFrm();
            frm.Rule = this.Rule;
            frm.IsSubPrjt = true;
            frm.MaterialId = Convert.ToInt32(xp.PrjtElement.material);
            frm.EditMode = false;
            frm.ShowDialog(this);
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            GRWL.BZSC.Common.EventCommon.PrdtPrjtUdTVEvnt -= new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(subPrjUpdate);
            GRWL.BZSC.Common.EventCommon.CopyPrjtUdTVEvnt -= new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(copyPrjtUpdate);
            GRWL.BZSC.Common.EventCommon.PrjtWorkstageUdTVEvnt -= new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(prjtWorkstageUpdate);
            GRWL.BZSC.Common.EventCommon.PrjtElementUdTVEvnt -= new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(PrjtElementUpdate);
        }

        /// <summary>
        /// 浏览子工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_ViewSubPrjt(object sender, EventArgs e)
        {
            if (this.tvRight.SelectedNode != null)
            {
                XPrjtElement peNode = (XPrjtElement)this.tvRight.SelectedNode.Tag;
                EditPrdtPrjtFrm frm = new EditPrdtPrjtFrm();
                frm.Id = Convert.ToInt32(peNode.NextPrjtId);
                frm.EditMode = true;
                frm.ShowDialog(this);
            }
        }

        /// <summary>
        /// 删除带子工程的元件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_DeleteElement2(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认删除该项目吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                XPrjtElement peNode = (XPrjtElement)this.tvRight.SelectedNode.Tag;

                if (MessageBox.Show("元件下面还挂有工程，是否删除？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    if (this.BaseDataProxy.DeletePrdtPrjt(Convert.ToInt32(peNode.NextPrjtId)))
                        this.tvRight.SelectedNode.Nodes.Clear();
                }

                TB_ProjectElement pe = this.BaseDataProxy.GetPrjtElement(peNode.PrjtElement.id);
                if (pe != null)//解除下级子工程关系
                {
                    pe.nextPrjt = null;
                    pe.nextPrjtCode = null;
                    this.BaseDataProxy.UpdatePrjtElement(pe);
                }

                if (this.BaseDataProxy.DeletePrjtElement(peNode.PrjtElement.id))
                    this.tvRight.SelectedNode.Parent.Nodes.RemoveAt(this.tvRight.SelectedNode.Index);
            }
        }


        /// <summary>
        /// 删除子工程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_DeleteSubPrjt(object sender, EventArgs e)
        {
            if (this.tvRight.SelectedNode != null)
            {
                XPrjtElement peNode = (XPrjtElement)this.tvRight.SelectedNode.Tag;
                if (MessageBox.Show("确认删除该子工程吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    peNode.PrjtElement.nextPrjt = null;
                    peNode.PrjtElement.nextPrjtCode = null;
                    if (this.BaseDataProxy.UpdatePrjtElement(peNode.PrjtElement))
                        bindRightPrjt(Convert.ToInt32(peNode.PrjtId));
                }
            }
        }

        /// <summary>
        /// 新增子工程工序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_AddSubPrjtWorkstage(object sender, EventArgs e)
        {
            if (this.tvRight.SelectedNode != null)
            {
                XPrjtElement peNode = (XPrjtElement)this.tvRight.SelectedNode.Tag;
                EditPrjtWorkstageFrm frm = new EditPrjtWorkstageFrm();
                //frm.Id = Convert.ToInt32(this.tvRight.SelectedNode.Name);
                frm.PrjtId = Convert.ToInt32(peNode.NextPrjtId);
                frm.PrjtCode = peNode.NextPrjtCode;
                frm.EditMode = false;
                frm.ShowDialog(this);
            }
        }

        /// <summary>
        /// 工程元件放产确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ctMenuClick_ReleaseElement(object sender, EventArgs e)
        {
            if (this.tvRight.SelectedNode != null)
            {
                TB_ProjectElement pe = this.BaseDataProxy.GetPrjtElement(Convert.ToInt32(this.tvRight.SelectedNode.Name));
                pe.isRelease = !this.ProductProject.isRelease;
                this.BaseDataProxy.UpdatePrjtElement(pe);
            }
        }
        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            bindAllPrjt();
            if (this.tvRight.Nodes[0] != null)
            { bindRightPrjt(Convert.ToInt32(this.tvRight.Nodes[0].Name)); }
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            FindPrdtPrjtFrm frm = new FindPrdtPrjtFrm(string.Empty);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                if (frm.SelectorItem != null)
                {
                    bindRightPrjt(frm.SelectorItem.Id);
                }
            }
        }

        private void btnCodeRule_Click(object sender, EventArgs e)
        {
            CodeRuleFrm frm = new CodeRuleFrm("产品工程编码规则");
            frm.Rule = this.Rule;
            if (frm.ShowDialog() == DialogResult.OK)
                this.codeRule = frm.Rule;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EditPrdtPrjtFrm frm = new EditPrdtPrjtFrm();
            frm.Rule = this.Rule;
            frm.ShowDialog(this);

        }
    }
}
